package ScalaDemo

class Music {
  var musiclist: List[String] = Nil
  var currentSong: String = ""
  var musicMode: String = "loop"

  def addSong(song: String): Unit = {
    musiclist = musiclist :+ song
  }

  def removeSong(song: String): Unit = {
    musiclist = musiclist.filter(_ != song)
  }

  def play(): Unit = {
    currentSong = musiclist.headOption.getOrElse("")
  }

  def next(): Unit = {

    musicMode match {
      case "loop" => loopPlayNext()
      case "shuffle" => shufflePlayNext()
      case "single" => singlePlayNext()
      case _ => println("Unknown play mode!")
    }
  }

  def loopPlayNext(): Unit = {
    val index = musiclist.indexOf(currentSong)
    if (index ==musiclist.length - 1) {
      currentSong = musiclist.headOption.getOrElse("")
    } else {
      currentSong = musiclist(index + 1)
    }
  }

  def shufflePlayNext(): Unit = {

    currentSong = scala.util.Random.shuffle(musiclist).headOption.getOrElse("")
  }

  def singlePlayNext(): Unit = {

    if (musiclist.contains(currentSong)) {
      currentSong = currentSong
    } else {
      currentSong = musiclist.headOption.getOrElse("")
    }
  }
}
